from django.db.models import QuerySet
from django.shortcuts import render,reverse

# Create your views here.
from django.shortcuts import render,redirect

# Create your views here.

# 创建基本框架
def f_top(request):
    return render(request, 'frame/top.html')

def f_left(request):
    return render(request, 'frame/left.html')

def f_right(request):
    return render(request, 'frame/right.html')

def f_frame(request):
    return render(request, 'frame/system_frame.html')


# 添加用户
def f_adduser(request):
    from app001.models import TUser,TIdcard
    # 获取所输入的用户信息
    if request.method=='POST':
        v_user_name=request.POST.get('user_name')
        v_user_password=request.POST.get('user_password')
        v_idcard=request.POST.get('card_num')   # 得到的是身份证号
        v_card_num = TIdcard.objects.create(idcard_num=v_idcard)
        TUser.objects.create(user_name=v_user_name,user_password=v_user_password,idcard_id=v_card_num.idcard_id)
    return redirect(reverse('app001:select'))

# 添加用户界面
def f_adduser_page(request):
    return render(request, 'userinfo/adduser_page.html')

# 查询用户
def f_select(request):
    from app001.models import TIdcard,TUser
    v_user_query=TUser.objects.all()
    return render(request,'userinfo/userinfo.html',{'v_user_query':v_user_query})

# 更新用户
def f_updateuser(request):
    from app001.models import TUser, TIdcard
    # 获取表单提交过来的数据
    if request.method=='POST':
        v_post_dict=request.POST
        v_id=v_post_dict.get('user_id',None)
        v_user=TUser.objects.get(user_id=v_id)
        v_user_name=request.POST.get('user_name')
        v_user_password = request.POST.get('user_password')
        v_user.user_name=v_user_name
        v_user.user_password=v_user_password
        v_user.save()
        #修改身份证
        v_card_num=v_post_dict.get('card_num',None)
        v_card=TIdcard.objects.get(idcard_id=v_user.idcard_id)
        v_card.idcard_num=v_card_num
        v_card.save()
        # 更新表中数据
    return redirect(reverse('app001:select'))

# 更新用户页面
def f_updateuser_page(request):
    # 获取传递过来的id值
    if request.method=='GET':
        v_dict=request.GET
        v_id_value=v_dict.get('id',None)
        from app001.models import TIdcard,TUser
        # 获取身份证表中信息不在用户表中的内容
        v_user_list=TUser.objects.all()
        v_user_id_list=[]
        for element in v_user_list:
            id=element.idcard_id
            v_user_id_list.append(id)
            pass
        v_card=TIdcard.objects.all()
        v_card_list=[]
        for element in v_card:
            id=element.idcard_id
            v_card_list.append(id)
            pass
        # 找出两个表之间的不同部分
        v_differ_list=list(set(v_card_list).difference(set(v_user_id_list)))
        v_list=[]
        for id in v_differ_list:
            v_card=TIdcard.objects.get(idcard_id=id)
            card_num=v_card.idcard_num
            v_list.append(card_num)
            pass
        v_user=TUser.objects.get(user_id=v_id_value)
        var_data_dict = {'userinfo_data': v_user,'card':v_list}
    return render(request,'userinfo/updateuser_page.html',context=var_data_dict)

# 删除用户
def f_deleteuser(request):
    if request.method=='GET':
        v_dict=request.GET
        v_id=v_dict.get('id',None)
        from app001.models import TIdcard,TUser
        v_user=TUser.objects.get(user_id=v_id)
        v_user.delete()
        v_card=TIdcard.objects.get(idcard_id=v_user.idcard_id)
        v_card.delete()
    return redirect(reverse('app001:select'))

def f_add_card(request):
    return render(request,'userinfo/add_card.html')


# 查询身份证信息
def f_selectcard(request):
    from app001.models import TIdcard,TUser
    v_card_query:QuerySet=TIdcard.objects.all()
    return render(request, 'cardinfo/cardinfo_all.html', {'v_card_query': v_card_query})

# 添加身份证信息
def f_add_card_page(request):
    return render(request, 'cardinfo/add_card_page.html')

def f_add_cardinfo(request):
    from app001.models import TIdcard
    if request.method=='POST':
        v_post_dict=request.POST
        v_card_num=v_post_dict.get('card_num',None)
        TIdcard.objects.create(idcard_num=v_card_num)
    return redirect(reverse('app001:selectcard'))


# 更新身份证
def f_update_card(request):
    from app001.models import TIdcard
    if request.method=='POST':
        v_card_dict=request.POST
        v_card_id=v_card_dict.get('card_id')
        v_card=TIdcard.objects.get(idcard_id=v_card_id)
        v_card_num=request.POST.get('card_num')
        v_card.idcard_num=v_card_num
        v_card.save()
    return redirect(reverse('app001:selectcard'))

def f_update_card_page(request):
    # 获取所要更新的身份证id
    if request.method=='GET':
        v_get_dict=request.GET
        v_card_id=v_get_dict.get('id',None)
        from app001.models import TIdcard
        v_card=TIdcard.objects.get(idcard_id=v_card_id)
        var_card_dict = {'userinfo_data': v_card}
    return render(request, 'cardinfo/card_update_page.html', context=var_card_dict)

# 删除身份证信息
def f_deletecard(request):
    from app001.models import TIdcard, TUser
    if request.method == 'GET':
        v_get_dict = request.GET
        v_id = v_get_dict.get('id', None)
        v_user_query = TUser.objects.all()
        v_list = []
        for element in v_user_query:
            id = element.idcard_id
            v_list.append(id)
        if int(v_id) in v_list:
            v_user = TUser.objects.get(idcard=v_id)
            v_user.idcard = None
            v_user.save()
            v_card = TIdcard.objects.get(idcard_id=v_id)
            v_card.delete()
        else:
            v_card = TIdcard.objects.get(idcard_id=v_id)
            v_card.delete()
    return redirect(reverse('app001:selectcard'))